package com.lablzh.gdxy.service.impl;

import com.lablzh.gdxy.component.DynamicOperateExcelUtils;
import com.lablzh.gdxy.entity.BonusProject;
import com.lablzh.gdxy.entity.Sciachievement;
import com.lablzh.gdxy.repository.BonusProjectRepository;
import com.lablzh.gdxy.service.BonusProjectService;
import com.lablzh.gdxy.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

@Service
public class BonusProjectServiceImpl implements BonusProjectService {
    private static final String BASEPATH = "./resources/static/";
    @Autowired
    private FileService fileService;
    @Autowired
    BonusProjectRepository bonusProjectRepositoty;

    @Override
    public String addbonusproject(BonusProject bonusProject) {
        bonusProjectRepositoty.save(bonusProject);
        return "添加成功";
    }

    @Override
    public String deletebonusproject(int id) {
        bonusProjectRepositoty.deleteById(id);
        return "删除成功";
    }

    @Override
    public String auditbonusproject(int id, String audit) {
        BonusProject bonusProject = new BonusProject();
        bonusProject = bonusProjectRepositoty.findById(id);
        bonusProject.setAudit(audit);
        bonusProjectRepositoty.save(bonusProject);
        return "审核成功";
    }

    @Override
    public BonusProject selectbonusproject(int id) {

        // BonusProject bonusProject=new BonusProject();

        return bonusProjectRepositoty.findById(id);
    }

    @Override
    public List<BonusProject> selectstudentbonusproject(String studentCode) {
        //   List<BonusProject> bonusProjectList=null;
        return bonusProjectRepositoty.findAllByStuCode(studentCode,Sort.by(Sort.Direction.DESC,"id"));
    }


    @Override
    public List<BonusProject> selectallbonusproject() {
        //  List<BonusProject> bonusProjectList=null;
        return bonusProjectRepositoty.findAll(Sort.by(Sort.Direction.DESC,"id"));
    }

    @Override
    public List<BonusProject> selectbonusProjectbyaudit(String audit) {
        return bonusProjectRepositoty.findAllByAudit(audit,Sort.by(Sort.Direction.DESC,"id"));
    }

    @Override
    public void downloadExcel(BonusProject bonusProject, HttpServletRequest request, String sort, HttpServletResponse response) throws FileNotFoundException {
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withMatcher("stuCode", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher(" name", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("major", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("classes", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("grade", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("projectName", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("miniType", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("audit", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("teacher", ExampleMatcher.GenericPropertyMatchers.contains());

        Example<BonusProject> example = Example.of(bonusProject, exampleMatcher);
        List<BonusProject> bonusProjectList = bonusProjectRepositoty.findAll(example);

        if (sort.equals("grade")) {
            bonusProjectList.sort(Comparator.comparing(BonusProject::getGrade));
        }  else if (sort.equals("studentUsername")) {
            bonusProjectList.sort(Comparator.comparing(BonusProject::getStuCode));
        } else if (sort.equals("major")) {
            bonusProjectList.sort(Comparator.comparing(BonusProject::getMajor));
        } else if (sort.equals("classes")) {
            bonusProjectList.sort(Comparator.comparing(BonusProject::getClasses));
        }
        DynamicOperateExcelUtils dynamicOperateExcelUtils = new DynamicOperateExcelUtils(BASEPATH + "奖学金加分学生汇总表模板.xlsx");
        List<Map<String, Object>> datas = new ArrayList<>();
        for (BonusProject project : bonusProjectList) {
            Map<String, Object> data = new HashMap<>();
            data.put("学号", project.getStuCode());
            data.put("姓名", project.getName());
            data.put("年级", project.getGrade());
            data.put("专业", project.getMajor());
            data.put("班级", project.getClasses());
            data.put("获奖项目名称", project.getProjectName());
            data.put("成果类型",project.getType());
            data.put("学生排名",project.getRanknumber());
            data.put("其他信息",project.getDetail());
            datas.add(data);
        }
        if (!datas.isEmpty()) {
            dynamicOperateExcelUtils.appendRows(5, datas,
                    new String[]{"学号", "姓名", "年级", "专业", "班级", "获奖项目名称", "成果类型",
                            "学生排名",  "其他信息"});
        } else {
            dynamicOperateExcelUtils.replaceCellValue(5, 0, "无相关记录");
        }
        dynamicOperateExcelUtils.exportExcel(new File(BASEPATH + "奖学金加分学生汇总表.xlsx"));
        fileService.downloadFile("奖学金加分学生汇总表.xlsx", request, response);
        fileService.deleteFile(BASEPATH + "奖学金加分学生汇总表.xlsx");
    }
}
